package edu.zhku.ttms.service.impl;

import edu.zhku.ttms.domain.Ticket;
import edu.zhku.ttms.mapper.TicketMapper;
import edu.zhku.ttms.service.TicketService;
import edu.zhku.ttms.utils.LoginSession;
import edu.zhku.ttms.utils.MybatisUtil;
import edu.zhku.ttms.utils.StringUtil;
import edu.zhku.ttms.utils.SystemConst;

import java.util.List;

/**
 * @author 林志强
 * @since 2021/4/19 11:08
 */

public class TicketServiceImpl implements TicketService {

    static TicketMapper ticketMapper = MybatisUtil.getMapper(TicketMapper.class);

    @Override
    public Ticket getById(Long ticketId) {
        return ticketMapper.getById(ticketId);
    }

    @Override
    public List<Ticket> getList(String str, Long policeId, Integer status) {
        return ticketMapper.getList(str, policeId, status);
    }

    @Override
    public int pay(Long ticketId) {
        Ticket ticket = ticketMapper.getById(ticketId);
        if (ticket == null) {
            return 0;
        }
        ticket.setStatus(1);
        return ticketMapper.updateOne(ticket);
    }

    @Override
    public String add(Ticket ticket) {
        if (StringUtil.isEmpty(ticket.getDriverLicenseNumber())) {
            return "司机驾驶证号不能为空！";
        }
        if (StringUtil.isEmpty(ticket.getViolationPlace())) {
            return "肇事地点不能未空！";
        }
        ticket.setPoliceId(LoginSession.getLoggedPoliceId());

        if (ticketMapper.insertOne(ticket) > 0) {
            return SystemConst.SUCCESS;
        }
        return "操作失败！";
    }

    @Override
    public String save(Ticket ticket) {
        if (StringUtil.isEmpty(ticket.getDriverLicenseNumber())) {
            return "司机驾驶证号不能为空！";
        }
        if (StringUtil.isEmpty(ticket.getViolationPlace())) {
            return "肇事地点不能未空！";
        }
        ticket.setPoliceId(LoginSession.getLoggedPoliceId());

        if (ticketMapper.updateOne(ticket) > 0) {
            return SystemConst.SUCCESS;
        }
        return "操作失败！";
    }

    @Override
    public int removeById(Long ticketId) {
        return ticketMapper.removeById(ticketId);
    }
}
